#! /bin/csh -f
#
# aqdrst: Script for running Aqua modules. 
# Modes:
#  - complete analysis for one restraint type
#  - cleanup
# =============================================================
# Ton Rullmann, Aug 18, 1997 - Aqua 1.4
# =============================================================
# This script assumes that the project name has been set.
# The variable 'aquaroot' must exist and point to the Aqua top directory.
# The 'aqsetup' script should reside in that directory.
# Exit value  0: OK
#             1: error
#            10: empty restraint file
#            30: no stereo definitions could be made
#
source $aquaroot/aqsetup

if ( $#argv < 1 ) then
    echo 'usage: aqdrst command ...'
    echo '   command = make | make_nodist] | stereo | clean | cleanall'
    echo '   restr_type = noe | hb | ss | dis'
    echo 'examples:'
    echo '   aqdrst  make[_nodist]   ident  coord_file  restr_file  restr_type  ...'
    echo '                     ... [ranges_file  ident_ranges]'
    echo '   aqdrst  stereo   assign|average|sum  [nmono]  ident  restr_type  ...'
    echo '       ... ident_stereo  [ranges_file  ident_ranges_normal ident_ranges_stereo]'
    echo '   aqdrst  clean    ident'
    echo '   aqdrst  cleanall'
    exit (1)
endif

set nodist = 0
set nmono = ""
set com = $1


# Set level of terseness as a category which can be modified later on
if ( ! $?AQUATERSENESS ) then
    echo "Environment variable: AQUATERSENESS is not defined" 
    echo "Assuming default value of 0 (most verbose status messages)"
    setenv AQUATERSENESS 0
endif

if ( $AQUATERSENESS < 3 ) then
    set terselevel = "verbose"
else if ( $AQUATERSENESS < 6 ) then
    set terselevel = "curbed"
else
    set terselevel = "terse"
endif

switch ( $com )

# MODE MAKE
case make:
case make_nodist:
    if ( $#argv != 5 && $#argv != 7 ) then
        echo 'usage: aqdrst make[_nodist] ident coord_file restr_file restr_type [ranges_file ident_ranges]'
        exit (1)
    endif
    if ( $1 == "make_nodist" ) set nodist = 1
    set ident    = $2
    set coordfil = $3
    set restrfil = $4
    set typ      = $5

#    if ( ! -e $coordfil ) then
#        echo '* Coordinate file' $coordfil 'does not exist'
#        exit (1)
#    endif
#    if ( ! -e $restrfil ) then
#        echo '* Restraint file' $restrfil 'does not exist'
#        exit (1)
#    endif

    if ( ! -e $AQUAPROJECT'_orig_'$ident.set ) then
        if ( $terselevel == "verbose" ) then
            echo '*'
            echo '* Processing coordinate file' $coordfil 'into Aqua file' 
	    echo '*'
        endif
	if ( `echo $coordfil | cut -c1 - ` == "%" ) then
	    qwhat set batch on exec aqua read $coordfil write $ident quit
	    if ( $status ) goto err
	else
	    qwhat set batch on exec aqua read $coordfil - write $ident quit
	    if ( $status ) goto err
	endif
    else
        if ( $terselevel == "verbose" ) then
            echo '*'
            echo '* Using existing Aqua coordinate file' $AQUAPROJECT'_orig_'$ident.set
	    echo '* instead of processing' $coordfil 
        endif
    endif
    echo ' '

    if ( $terselevel == "verbose" ) then
        echo "TIME qwhat  "
        time
        echo ' '
        echo '*'
        echo '* Processing restraint file' $restrfil
        echo '*'
    endif

    qhow set batch on exec aqua mol orig $ident $typ aqua $restrfil make $ident quit > qhow.tmp
    set errstatus = $status
    if ( $errstatus != 20 ) then
	cat qhow.tmp
	\rm qhow.tmp
    endif
    if ( $errstatus == 10 ) goto err10

    if ( $errstatus == 20 ) then
        echo ' '
        echo '*'
        echo '* Not all restraint atoms were found'
        echo '*'
        echo ' '
        if ( $terselevel == "verbose" ) then
	    echo "TIME qhow   "
	    time
	    echo ' '
        endif
	set newstatus = 0
        if ( ! -e $AQUAPROJECT'_explo_'$ident.set ) then
	    echo '*'
            echo '* Generating pseudo atoms'
	    echo '*'
            qpseudo set batch on exec aqua mol orig $ident make write explodedSet $ident set coordout pdb write pseudoSet $AQUAPROJECT'_pseudo.pdb' quit
	    set newstatus = $status
            if ( $newstatus == 10 ) then
		echo '*'
		echo '* No pseudo atoms could be made'
		echo '* Continuing with incomplete restraint results'
		echo '* Results of restraint processing were:'
		cat qhow.tmp
		echo '*'
	    else
		if ( $newstatus ) goto err
	    endif
        else
	    echo '*'
            echo '* Using existing Aqua pseudo-atom coordinate file' $AQUAPROJECT'_explo_'$ident.set
        endif
        echo ' '
        if ( $terselevel == "verbose" ) then
	    echo "TIME qpseudo"
	    time
	    echo ' '
        endif
	\rm qhow.tmp
	if ( $newstatus == 10 ) goto dodist

        echo '*'
        echo '* Processing restraint file again after adding pseudo atoms'
        echo '*'
        qhow set batch on exec aqua mol explo $ident $typ aqua $restrfil make $ident quit
        set newstatus = $status
	if ( $newstatus == 10 ) goto err10
        if ( $newstatus == 20 ) then
            echo '*'
            echo '* Again: not all restraint atoms were found. Please check their names.'
            echo '*'
	    goto dodist
        endif
        if ( $newstatus ) goto err
	goto dodist
    endif
    if ( $errstatus ) goto err	

dodist:
    echo ' '
    if ( $terselevel == "verbose" ) then
        echo "TIME qhow   "
        time
        echo ' '
    endif
    if ( $nodist ) breaksw

    qdist set batch on exec aqua do normal $typ $ident
    if ( $status ) goto err
    echo ' '
    if ( $terselevel == "verbose" ) then
        echo "TIME qdist  "
        time
        echo ' '
    endif
    if ( $#argv == 5 ) then
	qcalc set batch on exec aqua do normal $typ $ident
	set errstatus = $status
    else
	qcalc set batch on exec aqua range $6 do normal $typ $ident $7 $typ $7 
	set errstatus = $status
    endif
    if ( $errstatus ) goto err
    echo ' '
    if ( $terselevel == "verbose" ) then
        echo "TIME qcalc  "
        time
        echo ' '
    endif
breaksw


# MODE STEREO
case stereo:
    if ( $#argv != 5 && $#argv != 6 && $#argv != 8 && $#argv != 9 ) then
        echo 'usage: '
	echo '     aqdrst stereo assign|average|sum [nmono] ident restr_type ident_stereo ...'
        echo '                ... [ranges_file ident_ranges_normal ident_ranges_stereo]'
        exit (1)
    endif
    set stetyp    = $2
    if ( $stetyp == "sum" ) then
	set nmono     = $3
	set ident     = $4
	set typ       = $5
	set ident_ste = $6
    else
	set ident     = $3
	set typ       = $4
	set ident_ste = $5
    endif
    if ( $terselevel == "verbose" ) then
        echo '*'
        echo '* Processing restraint file again in "stereo" mode'
        echo '*'
    endif
    qhow set batch on exec aqua readinf $typ $ident stereo $stetyp $ident_ste quit
    set errstatus = $status
    if ( $errstatus == 30 ) goto err30
    if ( $errstatus ) goto err
    echo ' '
    if ( $terselevel == "verbose" ) then
        echo "TIME qhow   "
        time
        echo ' '
    endif
    qdist set batch on exec aqua do extended $typ $ident_ste
    if ( $status ) goto err
    echo ' '
    if ( $terselevel == "verbose" ) then
        echo "TIME qdist  "
        time
        echo ' '
    endif
    if ( $stetyp == "average" || $stetyp == "sum" ) then
	if ( $#argv == 5 || $#argv == 6 ) then
	    qcalc set batch on exec aqua do extended $typ $ident_ste $stetyp $nmono
	    set errstatus = $status
	else
	    if ( $stetyp == "average" ) then
		qcalc set batch on exec aqua range $6 do extended $typ $ident_ste $7 $8 $typ $8 $stetyp $nmono
		set errstatus = $status
	    else
		qcalc set batch on exec aqua range $7 do extended $typ $ident_ste $8 $9 $typ $9 $stetyp $nmono
		set errstatus = $status
	endif
	if ( $errstatus ) goto err
	echo ' '
        if ( $terselevel == "verbose" ) then
	   echo "TIME qcalc  "
	   time
	   echo ' '
        endif
    endif

breaksw


# MODE CLEAN
case clean:
    if ( $#argv < 2 ) then
	echo 'usage: aqdrst clean ident'
	exit (1)
    endif
    set proj  = $AQUAPROJECT
    set ident = $2
    \rm $proj'_'*'_'$ident.dinf $proj'_'*'_'$ident.dval $proj'_'*'_'$ident.drst
    if ( -e $proj'_orig_'$ident.set )  \rm $proj'_orig_'$ident.set 
    if ( -e $proj'_explo_'$ident.set ) \rm $proj'_explo_'$ident.set
breaksw


# MODE CLEANALL
case cleanall:
    if ( $#argv < 1 ) then
	echo 'usage: aqdrst cleanall'
	exit (1)
    endif
    echo "remove all these files:"
    \ls -l *_*_*.dinf *_*_*.dval *_*_*.drst *_orig_*.set *_explo_*.set
    echo "ok? (y/n)"
    set ans = $<
    if ( $ans == "y" || $ans == "Y" ) then
	\rm *_*_*.dinf *_*_*.dval *_*_*.drst *_orig_*.set *_explo_*.set
    else
	echo "files not removed"
    endif
breaksw

default:
    echo 'illegal command:' $1
    goto err
endsw

exit (0)

err:
echo ' * ERROR'
exit (1)

err10:
echo ' * ERROR'
exit (10)

err30:
echo ' * ERROR'
exit (30)
